import Vue from 'vue'
import { Message } from 'element-ui'
import request from '@/utils/request'
import axios from 'axios';

// 全局变量
const app = {

    state: {

        // 全局遮罩
        loading: false,

        // 全局页面开关
        pageReady: false,

        // 文件上传地址
        fileUpload: process.env.VUE_APP_API_FILE + "upload",
        // fileUpload: process.env.VUE_APP_API_MINIO_FILE + "upload",

        // 文件下载地址
        fileDownload: process.env.VUE_APP_API_FILE + "download",

        // 文件上传地址
        gisFileUpload: process.env.VUE_APP_API_GIS_FILE + "upload",

        // 文件下载地址
        gisFileDownload: process.env.VUE_APP_API_GIS_FILE + "download",

        // 缓存的用户信息
        userInfo: {app_title:""},

        // 缓存的系统变量
        systemConstant: "",

        // 缓存的字典值
        dict: {},

        // 字典值的加载标识
        dictLoading: {},

        // 缓存的功能标识
        fns: {},

        // 功能值加载的标识
        fnsLoading: {},

        // 系统通知
        notice: [],
        //消息列表--推送的消息
        layOutMsg:null,

    },

    mutations: {
        updateLoading(state, value) {
            state.loading = value;
        },
        updatePageReady(state, value) {
            state.pageReady = value;
        },
        updateUserInfo(state, value) {
            state.userInfo = value;
        },
        updateSystemConstant(state, value) {
            state.systemConstant = value;
        },
        updateDict(state, { type, dict }) {
            // 处理dict的v_code
            function editDict(src = []) {
                src.forEach(e => {
                    e.code = e.data.v_code;
                    if (e.children) {
                        editDict(e.children);
                    }
                });
            }
            editDict(dict);
            Vue.set(state.dict, type, dict);
        },
        updateDictLoading(state, { type, value }) {
            if (type) {
                Vue.set(state.dictLoading, type, value);
            }
        },
        updateFns(state, { key, fns }) {
            Vue.set(state.fns, key, fns);
        },
        updateFnsLoading(state, { key, value }) {
            Vue.set(state.fnsLoading, key, value);
        },
        updateOnlineNumber(state, value = 0) {
            state.onlineNumber = value;
        },
        updateNotice(state, value) {
            state.notice = value;
        },
        updateMsgList_commit(state, value) {
            state.layOutMsg = value;
        },
    },

    actions: {
        openBodyMask({ commit }) {
            commit('updateLoading', true);
        },
        closeBodyMask({ commit }) {
            commit('updateLoading', false);
        },
        pageReady({ commit }) {
            commit('updatePageReady', true);
        },
        logout() {
            window.location.href = '/logout';
        },
        updateSystemConstant({ state, commit }) {
            if (!state.systemConstant) {
                request({
                    url: "api/platform/dict/view",
                    method: "get",
                    params: { condition: "id_at_app_dictcategory ='PLATFORM'" }
                }).then(v => {
                    let constant = {};
                    v.list.forEach(d => {
                        constant[d.v_code] = d.v_value;
                    });
                    commit('updateSystemConstant', constant);
                });
            }
        },
        updateDict({ state, commit }, type) {
            if (!state.dict[type] && !state.dictLoading[type]) {
                commit('updateDictLoading', { type, value: true });
                request({
                    url: 'api/platform/dictcategory/view/' + type + '/children/app_dict',
                    method: 'get'
                }).then(dict => {
                    commit('updateDict', { type, dict });
                    commit('updateDictLoading', { type, value: false });
                });
            }
        },
        updateFns({ state, commit }, config) {
            let url, key;
            let headers = {};
            if (typeof(config) === 'object' && config.target && config.module) {
                url = config.target;
                key = config.target + "@" + config.module;
                headers.id_at_app_module = config.module;
            } else if (typeof(config) === 'string') {
                url = key = config;
            }
            if (url && key) {
                if (!state.fns[key] && !state.fnsLoading[key]) {
                    commit('updateFnsLoading', { key, value: true });
                    request({
                        url: url + '/functions',
                        method: 'get',
                        headers
                    }).then(fns => {
                        commit('updateFns', { key, fns });
                        commit('updateFnsLoading', { key, value: false });
                    });
                }
            }
        },
        updateMsgList({ state,commit }, action){
            request({
                url: "api/msg/app/inBox/" + state.userInfo.id,
                method: "get",
                params: {
                    b_readed: false
                }
            }).then(v =>{

                commit('updateMsgList_commit', v);
            })
        },
    },

    getters: {
        // 从字典树中获取目标
        getDict: state => (type, code) => {
            // 递归查询
            function recurByCode(src = [], c, result) {
                src.forEach(e => {
                    if (c && !result) {
                        if (e.code == c) {
                            result = e;
                        } else if (e.children) {
                            result = recurByCode(e.children, c, result);
                        }
                    }
                });
                return result;
            }
            let dictTree = state.dict[type];
            return recurByCode(dictTree, code);
        },
        // 获取字典列表
        getDictTree: state => type => {
            return state.dict[type];
        },
        // 获取功能列表
        hasFn: state => (config, sign) => {
            let key;
            if (typeof(config) === 'object' && config.target && config.module) {
                key = config.target + "@" + config.module;
            } else if (typeof(config) === 'string') {
                key = config;
            }
            let fns = state.fns[key];
            if (fns) {
                return _.indexOf(fns, '*') >= 0 || _.indexOf(fns, sign) >= 0;
            }
        },
        appTitle: state => state.userInfo.app_title || state.systemConstant.APP_NAME,
        appNotice: state => state.notice,
        appNoticeAlarm: state => _.find(state.notice, { alarm: true })
    }

}

export default app
